//
//  Abacus.m
//  Abacus
//  
//  Created by user on 11. 1. 30..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Abacus.h"
#import "Bead.h"


@implementation Abacus


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


//===============================================================
// 화면 Load 후
//===============================================================
CGPoint getPoint(int x, int y) {
	if (y == 0)
		return CGPointMake(WIDTH - 79, HEIGHT - BEAD_WIDTH * x - 70);
	else
		return CGPointMake(WIDTH - y * BEAD_HEIGHT - 128, HEIGHT - BEAD_WIDTH * x - 70);
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSLog(@"\n\n\n\n\n\n\n\n\n\n\n");
	beads = [[NSMutableArray alloc] init]; //[NSMutableArray arrayWithCapacity: CIPHER * 5];
	for (int x = 0; x < CIPHER; x++) {
		for (int y = 0; y < 5; y++) {
			CGRect dragRect = CGRectMake(0.0f, 0.0f, 40.0f, 65.0f);
			NSLog(@"%d %d %f", x, y, getPoint(x, y).x);
			dragRect.origin = getPoint(x, y);			
		
			Bead *bead = [[Bead alloc] initWithFrame:dragRect];
			
			// 초기 x, y좌표를 기억해둔다.
			[bead setDefault:x*5+y x:dragRect.origin.x y:dragRect.origin.y];
			
			[bead setImage:[UIImage imageNamed:@"bead.png"]];
			/*
			[bead setUserInteractionEnabled:YES];
			//bead.multipleTouchEnabled = YES;
			*/
			
			[self.view addSubview:bead];
			[beads addObject:bead];
			[bead release];	
		}
	}
	/*
	for (Bead *line in beads) {
		NSLog (@"%@", [line class]);
	}
	for (int i = 0 ; i < [ beads count]; ++i) {
		NSLog (@"%d => %@", i, [[beads objectAtIndex: i] class]);
	}
	 */
    [super viewDidLoad];
}

//===============================================================
// Touch 시작
//===============================================================
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchLocation = [touch locationInView:self.view];
	
	for (Bead *bead in beads) {
		if (touchLocation.x-bead.frame.origin.x <= BEAD_HEIGHT && 
			touchLocation.y-bead.frame.origin.y <= BEAD_WIDTH && 
			bead.frame.origin.x <= touchLocation.x &&
			bead.frame.origin.y <= touchLocation.y) {
			bead.posX = touchLocation.x;
			CGRect f = bead.frame;
			NSLog(@"*** BEGIN id:%d, posX:%d max:%d min:%d origin.x:%f", bead.uniqueId, bead.posX, bead.maxX, bead.minX, f.origin.x);
			break;
		}
	}
}

//===============================================================
// Touch Move
//===============================================================
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchLocation = [touch locationInView:self.view];
	
	int i = 0;
	for (Bead *bead in beads) {
		// 현재 선택된 주판알 알아내기
		if (touchLocation.x-bead.frame.origin.x <= BEAD_HEIGHT && 
			touchLocation.y-bead.frame.origin.y <= BEAD_WIDTH && 
			bead.frame.origin.x <= touchLocation.x &&
			bead.frame.origin.y <= touchLocation.y) {
			if (bead.posX == -1) bead.posX = touchLocation.x;
			CGRect f = bead.frame;
			// touchLocation.x : Mouse의 x 좌표
			NSLog(@"f.origin.x = %f", f.origin.x);
			f.origin.x += touchLocation.x - bead.posX;
			NSLog(@"%f += %f %d", f.origin.x, touchLocation.x, bead.posX);
			if (f.origin.x > bead.maxX) {
				NSLog(@"\nMove Max %f>%d %d", f.origin.x, bead.maxX, bead.uniqueId);
				f.origin.x = bead.maxX;
			} else if (f.origin.x < bead.minX) {
				NSLog(@"\nMove Min %f<%d %d", f.origin.x, bead.minX, bead.uniqueId);
				f.origin.x = bead.minX;
				
			}
			bead.frame = f;
		} else {
			bead.posX = -1;
		}
		i++;
	}
	
	// [self touchesBegan:touches withEvent:event];
	// 위아래만 이동
}

//===============================================================
// Touch Move
// 반이상 이동시 완전이동으로 처리 또는 원위치
//===============================================================
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	return;
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchLocation = [touch locationInView:self.view];
	NSLog(@"\n\ntouchEND==========");
	for (Bead *bead in beads) {
		// 최초 마우스 클릭 위치 초기화
		bead.posX = -1;
		if (touchLocation.x-bead.frame.origin.x <= BEAD_HEIGHT && 
			touchLocation.y-bead.frame.origin.y <= BEAD_WIDTH && 
			bead.frame.origin.x <= touchLocation.x &&
			bead.frame.origin.y <= touchLocation.y) {
			// 반이상 이동시 완전이동으로 처리 또는 원위치
			CGRect f = bead.frame;
			
			if (f.origin.x != bead.minX && f.origin.x != bead.maxX) {
				NSLog(@"End Move %d", bead.uniqueId);
				if (f.origin.x > (bead.minX + MAX_MOVE / 2)) {
					NSLog(@"End Max");
					f.origin.x = bead.maxX;
				} else {
					NSLog(@"End Min");
					f.origin.x = bead.minX;
				}
				bead.frame = f;
				// 소리를 내도록 한다.
			}
		}
	}
	

}
/*
-(Bead)getCurrentBead:(int) x y:(int) y {
	for (Bead *bead in beads) {
		if (x-bead.frame.origin.x <= BEAD_HEIGHT && y-bead.frame.origin.y <= BEAD_WIDTH 
			&& 	bead.frame.origin.x <= x &&	bead.frame.origin.y <= y) {
			return bead;
		}
	}
	return nil;
}
*/
//===============================================================
// Touch 취소
//===============================================================
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	// 진행하기 전단계로 모두 이동
}

- (void)dealloc {
    [super dealloc];
}


@end
